package com.example.win.android_banduyujia.ui.seek;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * Created by Win on 2015/9/12.
 */
public class SeekCircle extends ProgressCircle {

    public interface OnSeekCircleChangeListener{
        void onProgressChanged(SeekCircle seekCircle, int progress, boolean fromUser);
        void onStartTrackingTouch(SeekCircle seekCircle);
        void onStopTrackingTouch(SeekCircle seekCircle);
    }

    private OnSeekCircleChangeListener mOnSeekCircleChangeListener;

    public void setOnSeekCircleChangeListener(OnSeekCircleChangeListener listener) {
        mOnSeekCircleChangeListener = listener;
    }

    private boolean mTrackingTouch = false;
    private int mRevolutions = 0;
    private float mOldX;

    public SeekCircle(Context context) {
        super(context);
    }

    public SeekCircle(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SeekCircle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
         float x = event.getX() - mCenterX;
         float y = mCenterY - event.getY();

         float distance = (float)Math.sqrt(x * x + y * y);

         boolean inRange = Math.abs(distance - mRadius) <= mSectionHeight;
         boolean inDeadZone = false;

         boolean updateProgress = false;

         switch (event.getAction()){
             case MotionEvent.ACTION_DOWN:
                  if(inRange){
                      mTrackingTouch = true;
                      mOldX = x;
                      mRevolutions = 0;
                      updateProgress = true;
                  }
                 break;
             case MotionEvent.ACTION_MOVE:
                   if(mTrackingTouch && !inDeadZone)
                       updateProgress = true;
                 break;
             case MotionEvent.ACTION_UP:
                   if(mTrackingTouch && !inDeadZone)
                        updateProgress = true;
                 mTrackingTouch = false;
                 break;
             case MotionEvent.ACTION_CANCEL:
                 mTrackingTouch = false;
                 break;
         }

        if(updateProgress){
            float position=(float)((Math.atan2(-x, -y) + Math.PI) / (Math.PI * 2.0));
            int  progress = Math.round(position * ((float)mMaxProgress));

            if(event.getAction() != MotionEvent.ACTION_DOWN){
                updateRevolutions(x, y);

                float absPosition=(float)mRevolutions + position;

                if(absPosition < 0.0f)
                    progress = 0;
                else if(absPosition > 1.0f)
                    progress = mMaxProgress;
            }
            mOldX = x;
            updateTouchProgress(progress);

            return true;
        }

        return super.onTouchEvent(event);
    }

    private void updateRevolutions(float x, float y) {
        boolean leftFlip=y > 1.0f && mOldX >= 0.0f && x < 0.0f;
        boolean rightFlip=y > 0.0f && mOldX <= 0.0f && x > 0.0f;

        if(leftFlip)
            mRevolutions -= 1.0f;
        else if(rightFlip)
            mRevolutions += 1.0f;

        mRevolutions = Math.max(-1, Math.min(1, mRevolutions));
    }

    private void updateTouchProgress(int progress) {
        boolean result=super.updateProgress(progress);
        if(result){
            if(mOnSeekCircleChangeListener !=null)
                mOnSeekCircleChangeListener.onProgressChanged(this, progress, true);
        }
    }
}
